package com.llkj.qingnianguoxue.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.ZhiBoListBean;
import com.llkj.qingnianguoxue.util.ClickViewCallback;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播专栏列表
 */
public class ZhiBoAdapter extends BaseQuickAdapter<ZhiBoListBean.ZhiBoBean, BaseViewHolder> {
    private ClickViewCallback clickListener;
    private List<TextView> mCountDownList = new ArrayList<>();
    private final int ITEM_LAYOUT_GOING = 22;

    public ZhiBoAdapter(int layoutResId, List<ZhiBoListBean.ZhiBoBean> data, ClickViewCallback listener) {
        super(layoutResId, data);
        clickListener = listener;
    }

    @Override
    protected int getDefItemViewType(int position) {
        ZhiBoListBean.ZhiBoBean item = mData.get(position);
        int type = Integer.parseInt(item.getState());//状态1 直播中 2 未开始 3 已结束
        if(position==0){
            return ITEM_LAYOUT_GOING;
        }
        return super.getDefItemViewType(position);
    }
    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_LAYOUT_GOING)
            return new TextViewHolder(getItemView(R.layout.item_zhibo_going, parent));

        return super.onCreateDefViewHolder(parent, viewType);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, ZhiBoListBean.ZhiBoBean item) {
        FrameLayout titleView = baseViewHolder.getView(R.id.fl_title);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) titleView.getLayoutParams();
        params.width = DisplayUtil.getDensityWdith(mContext);
        ImageView ivPic = baseViewHolder.getView(R.id.iv_action_pic);
        FrameLayout.LayoutParams picParams = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
        String time = item.getLive_time();
        if (baseViewHolder.getItemViewType() == ITEM_LAYOUT_GOING){
            time = "直播时间：" + time;
            params.height = params.width * (272+ DisplayUtil.dip2px(mContext,50)) / 750;
            picParams.height = params.height;
        }else {
            params.height = params.width * 272 / 750;
            picParams.height = params.height;
            baseViewHolder.setText(R.id.tv_price, String.format(mContext.getString(R.string.pay_format), String.valueOf(50.00)));
        }

        ImageLoadingUtil.loadingUrl(ivPic, item.getImg());

        baseViewHolder.setText(R.id.tv_title, item.getTitle());
        baseViewHolder.setText(R.id.tv_date,time);

        TextView tvState = baseViewHolder.getView(R.id.tv_state);
        if(mCountDownList.size()<=baseViewHolder.getLayoutPosition()){
            mCountDownList.add(tvState);
        }else{
            mCountDownList.set(baseViewHolder.getLayoutPosition(),tvState);
        }

        int type = Integer.parseInt(item.getState());//状态1 直播中 2 未开始 3 已结束
        switch (type) {
            case 1:
                tvState.setSelected(false);
                if ("1".equals(item.getIs_order())) {//如果预约
                    tvState.setText("进入直播");
                } else {
                    tvState.setText("正在直播");
                }
                break;
            case 2:
                if ("1".equals(item.getIs_order())) {//如果预约
                    //如果预约
//                    String time = calculateTime(baseViewHolder.getLayoutPosition(),item);
//                    tvState.setText(time);
                    tvState.setSelected(true);
                } else {
                    tvState.setText("预约参与");
                    tvState.setSelected(true);
                    clickLister(tvState, baseViewHolder.getLayoutPosition(), 0);
                }
                break;
            case 3:
                tvState.setText("直播回看");
                tvState.setSelected(false);
                break;
        }

        FrameLayout flTitle = baseViewHolder.getView(R.id.fl_title);
        clickLister(flTitle, baseViewHolder.getLayoutPosition(), 1);
    }

    /**
     * 计算倒计时
     * @param position
     * @param item
     * @return
     */
    public String calculateTime(int position, ZhiBoListBean.ZhiBoBean item){
        String time = "已预约";
        try {
            long timeLong = Long.parseLong(item.getCountdown());
            timeLong--;
            item.setCountdown(String.valueOf(timeLong));
            if(timeLong<=0){
                item.setState("1");
                notifyItemChanged(position);
                return time;
            }
            time = "";
            int day = (int) (timeLong / (24 * 60 * 60));
            int hours = (int) ((timeLong%(24 * 60 * 60)) / (60 * 60));
            int minutes = (int) ((timeLong%(24 * 60 * 60) % (60 * 60)) / (60));
            int seconds = (int) ((timeLong % (60)));

            if (day > 0) {
                time = day + "天 ";
            }
            time = time + hours + ":" + minutes + ":" + seconds;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }
    private void clickLister(final View view, final int position, final int type) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.clickViewCallback(position, type, view);
                }
            }
        });
    }

    public List<TextView> getCountDownList(){
        return mCountDownList;
    }

    public class TextViewHolder extends BaseViewHolder {
        public TextViewHolder(View itemView) {
            super(itemView);
        }
    }
}
